package com.iedu.demo.lgq.service.impl;

import com.iedu.demo.lgq.entity.TbItem;
import com.iedu.demo.lgq.mapper.ItemMapper;
import com.iedu.demo.lgq.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public List<TbItem> getAllItems() {
        return itemMapper.findAllItems();
    }

    @Override
    public List<TbItem> searchItems(String title, String status) {
        return itemMapper.findItemsByCondition(title, status);
    }

    @Override
    public TbItem getItemById(String itemId) {
        return itemMapper.selectById(itemId);
    }

    @Override
    public void uploadItem(TbItem item) {
        itemMapper.addItem(item);
    }

    @Override
    public void editItem(TbItem item) {
        itemMapper.updateItem(item);
    }

    @Override
    public void deleteItem(String itemId) {
        itemMapper.deleteItem(itemId);
    }

    @Override
    public void updateItemStock(String id, String num) {
        itemMapper.setItemStock(id, num);
    }


    @Override
    public List<TbItem> getItemList(int pageNum, int pageSize) {
        // 计算从哪一项开始
        int offset = (pageNum - 1) * pageSize;
        return itemMapper.getItemList(offset, pageSize);
    }

    @Override
    public List<TbItem> getItemsByCategory(String categoryId, String keyword, int pageNum, int pageSize) {
        // 计算分页的偏移量
        int offset = (pageNum - 1) * pageSize;
        return itemMapper.getItemsByCategory(categoryId, "%" + keyword + "%", offset, pageSize);
    }

}
